package viewmodel;

import android.arch.core.util.Function;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
import java.util.List;
import javax.inject.Inject;
import model.redpack.Pack;
import model.redpack.PackListRsp;
import repository.RedPackRepository;

public class RedPackViewModel extends BaseViewModel<RedPackRepository> {
    RedPackRepository repository;
    @Inject
    public RedPackViewModel(RedPackRepository repository) {
        this.repository = repository;
    }

    @Override
    protected RedPackRepository getRepository() {
        return repository;
    }

    public LiveData<List<Pack>> getRedPackList() {
        MutableLiveData<PackListRsp>  data=repository.getRedPackList();
        LiveData<List<Pack>> liveData=Transformations.switchMap(data, new Function<PackListRsp, LiveData<List<Pack>>>() {
            @Override
            public LiveData<List<Pack>> apply(PackListRsp input) {
                MediatorLiveData mediatorLiveData=new MediatorLiveData<List<Pack>>();
                mediatorLiveData.setValue(input.getData());
                return mediatorLiveData;
            }
        });
        return liveData;
    }



}
